package uba.ing2.subite.services.impl;

import java.util.HashSet;
import java.util.Set;

import uba.ing2.subite.domain.Account;
import uba.ing2.subite.domain.DisposableCard;
import uba.ing2.subite.domain.PaymentCard;
import uba.ing2.subite.domain.PrepaidCard;
import uba.ing2.subite.services.Clock;
import uba.ing2.subite.services.PaymentCardService;

public class PaymentCardServiceImpl implements PaymentCardService {
	private Clock clock;
	private int lastDisposableCardNumber = 1;
	private int lastPrepaidCardNumber = 1;
	private Set<PaymentCard> validCards = new HashSet<PaymentCard>();

	public PaymentCardServiceImpl(Clock clock) {
		this.clock = clock;
	}

	@Override
	public PaymentCard newDisposableCardWith(Number disposableCardInitalMoney) {
		return registerCard(new DisposableCard("X-" + (lastDisposableCardNumber++), disposableCardInitalMoney));
	}

	private PaymentCard registerCard(PaymentCard card) {
		validCards.add(card);
		return card;
	}

	@Override
	public PaymentCard newPrepaidCardFor(Account account) {
		return registerCard(new PrepaidCard("P-" + (lastPrepaidCardNumber ++), account.identifier(), account.balanceOn(clock.currentTime())));
	}

	@Override
	public boolean canAcceptPaymentsWith(PaymentCard card) {
		return validCards.contains(card);
	}

}
